package com.rbs.service.impl;

import com.rbs.entity.Category;
import com.rbs.mapper.CategoryMapper;
import com.rbs.service.CategoryService;
import com.rbs.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    @Override
    public List<Category> selectAllCategory() {
        SqlSession session = null;
        List<Category> categories = null;
        try {
            session = MybatisUtils.getSession();
            CategoryMapper mapper = session.getMapper(CategoryMapper.class);
            categories = mapper.selectAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return categories;
    }

    @Override
    public Category selectCategoryById(int id) {
        SqlSession session = null;
        Category category = null;
        try {
            session = MybatisUtils.getSession();
            CategoryMapper mapper = session.getMapper(CategoryMapper.class);
            category = mapper.selectCategoryById(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return category;
    }

    @Override
    public void deleteCategoryById(int id) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            CategoryMapper mapper = session.getMapper(CategoryMapper.class);
            mapper.deleteCategory(id);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }
}
